package com.example.demo.controller;

import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Customer;
import com.example.demo.entity.OrderItem;
import com.example.demo.entity.Orders;
import com.example.demo.entity.User;
import com.example.demo.service.CustomerService;
import com.example.demo.service.OrderItemService;
import com.example.demo.service.OrdersService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/orderItem")
public class OrderItemController {
    @Autowired
    OrderItemService orderItemService;

    @Autowired
    OrdersService ordersService;

    @Autowired
    CustomerService customerService;
    @Autowired
    UserService userService;
    @PostMapping
    public Result save(@RequestBody OrderItem orderItem) {
        if (orderItem.getId() == null) {
            //orderItem.setTime(DateUtil.now());
            //orderItem.setUser(TokenUtils.getCurrentUser().getUsername());
        }
        orderItemService.saveOrUpdate(orderItem);
        return Result.suc();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        orderItemService.removeById(id);
        return Result.suc();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        orderItemService.removeByIds(ids);
        return Result.suc();
    }

    @GetMapping
    public Result findAll() {
        return Result.suc(orderItemService.list());
    }

    @GetMapping("/comment/{goodsId}")
    public Result comment(@PathVariable Integer goodsId) {
        ArrayList<Object> comments = new ArrayList<>();
        List<OrderItem> list = orderItemService.list(new QueryWrapper<OrderItem>().eq("goods_id", goodsId));
        for (OrderItem orderItem : list) {
            Integer orderId = orderItem.getOrderId();
            Orders orders = ordersService.getById(orderId);
            Integer userid = orders.getUserid();
            Customer customer = customerService.getById(userid);
            if (StrUtil.isNotBlank(orderItem.getComment())) {
                comments.add(Dict.create().set("user", customer.getName()).set("comment", orderItem.getComment()));
            }
        }
        return Result.suc(comments);
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.suc(orderItemService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "") String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(name)) {
            queryWrapper.like("name", name);
        }
//        User currentUser = TokenUtils.getCurrentUser();
//        if (currentUser.getRole().equals("ROLE_USER")) {
//            queryWrapper.eq("user", currentUser.getUsername());
//        }
        return Result.suc(orderItemService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}
